package com.zhaodui.loan.controller;
import com.zhaodui.loan.entity.FxjLoanFkEntity;
import com.zhaodui.loan.entity.FxjLoanMaEntity;
import com.zhaodui.loan.service.FxjLoanMaServiceI;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhaodui.order.entity.FxjOrderLogEntity;
import com.zhaodui.order.entity.FxjOrderMainEntity;
import com.zhaodui.util.fxjUtil;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.pojo.base.TSUserOrg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import java.io.OutputStream;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.core.util.ResourceUtil;
import java.io.IOException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.util.Map;
import java.util.HashMap;
import org.jeecgframework.core.util.ExceptionUtil;

import org.springframework.stereotype.Controller;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.jeecgframework.core.beanvalidator.BeanValidators;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.net.URI;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;
import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.jeecgframework.web.cgform.entity.upload.CgUploadEntity;
import org.jeecgframework.web.cgform.service.config.CgFormFieldServiceI;
import java.util.HashMap;

import static com.xiaoleilu.hutool.date.DateTime.now;

/**
 * @Title: Controller  
 * @Description: 融资物管理
 * @author onlineGenerator
 * @date 2018-11-30 14:40:01
 * @version V1.0   
 *
 */
@Api(value="FxjLoanMa",description="融资物管理",tags="fxjLoanMaController")
@Controller
@RequestMapping("/fxjLoanMaController")
public class FxjLoanMaController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(FxjLoanMaController.class);

	@Autowired
	private FxjLoanMaServiceI fxjLoanMaService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;
	@Autowired
	private CgFormFieldServiceI cgFormFieldService;
	


	/**
	 * 融资物管理列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/loan/fxjLoanMaList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(FxjLoanMaEntity fxjLoanMa,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(FxjLoanMaEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, fxjLoanMa, request.getParameterMap());
		try{
		//自定义追加查询条件
		
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.fxjLoanMaService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * 删除融资物管理
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(FxjLoanMaEntity fxjLoanMa, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		fxjLoanMa = systemService.getEntity(FxjLoanMaEntity.class, fxjLoanMa.getId());
		message = "融资物管理删除成功";
		try{
			fxjLoanMaService.delete(fxjLoanMa);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "融资物管理删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 批量删除融资物管理
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "融资物管理删除成功";
		try{
			for(String id:ids.split(",")){
				FxjLoanMaEntity fxjLoanMa = systemService.getEntity(FxjLoanMaEntity.class, 
				Integer.parseInt(id)
				);
				fxjLoanMaService.delete(fxjLoanMa);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "融资物管理删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加融资物管理
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(FxjLoanMaEntity fxjLoanMa, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "融资物管理添加成功";
		try{
			fxjLoanMaService.save(fxjLoanMa);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "融资物管理添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 更新融资物管理
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(FxjLoanMaEntity fxjLoanMa, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "融资物管理更新成功";
		FxjLoanMaEntity t = fxjLoanMaService.get(FxjLoanMaEntity.class, fxjLoanMa.getId());
		try {
			MyBeanUtils.copyBeanNotNull2Bean(fxjLoanMa, t);
			fxjLoanMaService.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "融资物管理更新失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	

	/**
	 * 融资物管理新增页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(FxjLoanMaEntity fxjLoanMa, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(fxjLoanMa.getId())) {
			fxjLoanMa = fxjLoanMaService.getEntity(FxjLoanMaEntity.class, fxjLoanMa.getId());
			req.setAttribute("fxjLoanMa", fxjLoanMa);
		}
		return new ModelAndView("com/zhaodui/loan/fxjLoanMa-add");
	}
	/**
	 * 融资物管理编辑页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(FxjLoanMaEntity fxjLoanMa, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(fxjLoanMa.getId())) {
			fxjLoanMa = fxjLoanMaService.getEntity(FxjLoanMaEntity.class, fxjLoanMa.getId());
			req.setAttribute("fxjLoanMa", fxjLoanMa);
		}
		return new ModelAndView("com/zhaodui/loan/fxjLoanMa-update");
	}
	
	/**
	 * 导入功能跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "upload")
	public ModelAndView upload(HttpServletRequest req) {
		req.setAttribute("controller_name","fxjLoanMaController");
		return new ModelAndView("common/upload/pub_excel_upload");
	}
	
	/**
	 * 导出excel
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXls")
	public String exportXls(FxjLoanMaEntity fxjLoanMa,HttpServletRequest request,HttpServletResponse response
			, DataGrid dataGrid,ModelMap modelMap) {
		CriteriaQuery cq = new CriteriaQuery(FxjLoanMaEntity.class, dataGrid);
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, fxjLoanMa, request.getParameterMap());
		List<FxjLoanMaEntity> fxjLoanMas = this.fxjLoanMaService.getListByCriteriaQuery(cq,false);
		modelMap.put(NormalExcelConstants.FILE_NAME,"融资物管理");
		modelMap.put(NormalExcelConstants.CLASS,FxjLoanMaEntity.class);
		modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("融资物管理列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
			"导出信息"));
		modelMap.put(NormalExcelConstants.DATA_LIST,fxjLoanMas);
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}
	/**
	 * 导出excel 使模板
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXlsByT")
	public String exportXlsByT(FxjLoanMaEntity fxjLoanMa,HttpServletRequest request,HttpServletResponse response
			, DataGrid dataGrid,ModelMap modelMap) {
    	modelMap.put(NormalExcelConstants.FILE_NAME,"融资物管理");
    	modelMap.put(NormalExcelConstants.CLASS,FxjLoanMaEntity.class);
    	modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("融资物管理列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
    	"导出信息"));
    	modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
    	return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(params = "importExcel", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(1);
			params.setNeedSave(true);
			try {
				List<FxjLoanMaEntity> listFxjLoanMaEntitys = ExcelImportUtil.importExcel(file.getInputStream(),FxjLoanMaEntity.class,params);
				for (FxjLoanMaEntity fxjLoanMa : listFxjLoanMaEntitys) {
					fxjLoanMaService.save(fxjLoanMa);
				}
				j.setMsg("文件导入成功！");
			} catch (Exception e) {
				j.setMsg("文件导入失败！");
				logger.error(ExceptionUtil.getExceptionMessage(e));
			}finally{
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return j;
	}
	
	/**
	 * 获取文件附件信息
	 * 
	 * @param id fxjLoanMa主键id
	 */
	@RequestMapping(params = "getFiles")
	@ResponseBody
	public AjaxJson getFiles(String id){
		List<CgUploadEntity> uploadBeans = cgFormFieldService.findByProperty(CgUploadEntity.class, "cgformId", id);
		List<Map<String,Object>> files = new ArrayList<Map<String,Object>>(0);
		for(CgUploadEntity b:uploadBeans){
			String title = b.getAttachmenttitle();//附件名
			String fileKey = b.getId();//附件主键
			String path = b.getRealpath();//附件路径
			String field = b.getCgformField();//表单中作为附件控件的字段
			Map<String, Object> file = new HashMap<String, Object>();
			file.put("title", title);
			file.put("fileKey", fileKey);
			file.put("path", path);
			file.put("field", field==null?"":field);
			files.add(file);
		}
		AjaxJson j = new AjaxJson();
		j.setObj(files);
		return j;
	}
	@RequestMapping(value="/list/{username}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="融资物管理列表信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<FxjLoanMaEntity>> list(@PathVariable("username") String username, @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize > Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		CriteriaQuery query = new CriteriaQuery(FxjLoanMaEntity.class);

		TSUser tsUser = systemService.findUniqueByProperty(TSUser.class,"userName",username);
		String orgcode = "";
		try{
			if(StringUtil.isNotEmpty(request.getParameter("fiName"))){
				query.like("fiName","%"+request.getParameter("fiName")+"%");
			}

			if(StringUtil.isNotEmpty(request.getParameter("orderNo"))){
				query.like("orderNo","%"+request.getParameter("orderNo")+"%");
			}

			if(StringUtil.isNotEmpty(request.getParameter("bpmStatus"))){
				query.eq("bpmStatus",request.getParameter("bpmStatus"));
			}
			if (StringUtil.getStringLen(username)>20){
				query.eq("createBy",username);
			}else {
				TSUser task = fxjUtil.getsysorgcode(username);
				if (task != null) {
					orgcode = task.getCurrentDepart().getSysOrgCode();
				}
 				query.like("sysOrgCode", orgcode + "%");
 			}
//			TSUser task = fxjUtil.getsysorgcode(username);
//
//			if(task!=null){
//				query.eq("diyaUser",task.getUserName());
//			}
//			if(task!=null){
//				query.eq("guohuUser",task.getUserName());
//			}

		}catch (Exception e){
		}
		query.add();
		List<FxjLoanMaEntity> listsize = this.fxjLoanMaService.getListByCriteriaQuery(query,false);
		query.setCurPage(pageNumber<=0?1:pageNumber);
		query.setPageSize(pageSize<1?1:pageSize);
		query.add();
		List<FxjLoanMaEntity> listFxjLoanMas = this.fxjLoanMaService.getListByCriteriaQuery(query,true);
		return Result.success(listFxjLoanMas,(long)listsize.size());
	}
	@RequestMapping(value="/listdy/{username}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="融资物管理列表信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<FxjLoanMaEntity>> listdy(@PathVariable("username") String username, @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize > Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		CriteriaQuery query = new CriteriaQuery(FxjLoanMaEntity.class);

		TSUser tsUser = systemService.findUniqueByProperty(TSUser.class,"userName",username);
		String orgcode = "";
		try{
			if(StringUtil.isNotEmpty(request.getParameter("fiName"))){
				query.like("fiName","%"+request.getParameter("fiName")+"%");
			}

			if(StringUtil.isNotEmpty(request.getParameter("orderNo"))){
				query.like("orderNo","%"+request.getParameter("orderNo")+"%");
			}

			if(StringUtil.isNotEmpty(request.getParameter("bpmStatus"))){
				query.eq("bpmStatus",request.getParameter("bpmStatus"));
			}
//			if (StringUtil.getStringLen(username)>20){
//				query.eq("createBy",username);
//			}else {
//				TSUser task = fxjUtil.getsysorgcode(username);
//				if (task != null) {
//					orgcode = task.getCurrentDepart().getSysOrgCode();
//				}
// 				query.like("sysOrgCode", orgcode + "%");
// 			}
			TSUser task = fxjUtil.getsysorgcode(username);

			if(task!=null){
				query.eq("diyaUser",task.getUserName());
			}
			query.eq("maBy1","N");
//			if(task!=null){
//				query.eq("guohuUser",task.getUserName());
//			}

		}catch (Exception e){
		}
		query.add();
		List<FxjLoanMaEntity> listsize = this.fxjLoanMaService.getListByCriteriaQuery(query,false);
		query.setCurPage(pageNumber<=0?1:pageNumber);
		query.setPageSize(pageSize<1?1:pageSize);
		query.add();
		List<FxjLoanMaEntity> listFxjLoanMas = this.fxjLoanMaService.getListByCriteriaQuery(query,true);

		return Result.success(listFxjLoanMas,(long)listsize.size());

	}

	@RequestMapping(value="/listdya/{username}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="融资物管理列表信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<FxjLoanMaEntity>> listdya(@PathVariable("username") String username, @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize > Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		CriteriaQuery query = new CriteriaQuery(FxjLoanMaEntity.class);

		TSUser tsUser = systemService.findUniqueByProperty(TSUser.class,"userName",username);
		String orgcode = "";
		try{
			if(StringUtil.isNotEmpty(request.getParameter("fiName"))){
				query.like("fiName","%"+request.getParameter("fiName")+"%");
			}

			if(StringUtil.isNotEmpty(request.getParameter("orderNo"))){
				query.like("orderNo","%"+request.getParameter("orderNo")+"%");
			}

			if(StringUtil.isNotEmpty(request.getParameter("bpmStatus"))){
				query.eq("bpmStatus",request.getParameter("bpmStatus"));
			}
//			if (StringUtil.getStringLen(username)>20){
//				query.eq("createBy",username);
//			}else {
//				TSUser task = fxjUtil.getsysorgcode(username);
//				if (task != null) {
//					orgcode = task.getCurrentDepart().getSysOrgCode();
//				}
// 				query.like("sysOrgCode", orgcode + "%");
// 			}
			TSUser task = fxjUtil.getsysorgcode(username);

			if(task!=null){
				query.eq("diyaUser",task.getUserName());
			}
//			query.eq("maBy1","N");
//			if(task!=null){
//				query.eq("guohuUser",task.getUserName());
//			}

		}catch (Exception e){
		}
		query.add();
		List<FxjLoanMaEntity> listsize = this.fxjLoanMaService.getListByCriteriaQuery(query,false);
		query.setCurPage(pageNumber<=0?1:pageNumber);
		query.setPageSize(pageSize<1?1:pageSize);
		query.add();
		List<FxjLoanMaEntity> listFxjLoanMas = this.fxjLoanMaService.getListByCriteriaQuery(query,true);

		return Result.success(listFxjLoanMas,(long)listsize.size());

	}

	@RequestMapping(value="/listgh/{username}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="融资物管理列表信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<FxjLoanMaEntity>> listgh(@PathVariable("username") String username, @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize > Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		CriteriaQuery query = new CriteriaQuery(FxjLoanMaEntity.class);

		TSUser tsUser = systemService.findUniqueByProperty(TSUser.class,"userName",username);
		String orgcode = "";
		try{
			if(StringUtil.isNotEmpty(request.getParameter("fiName"))){
				query.like("fiName","%"+request.getParameter("fiName")+"%");
			}

			if(StringUtil.isNotEmpty(request.getParameter("orderNo"))){
				query.like("orderNo","%"+request.getParameter("orderNo")+"%");
			}

			if(StringUtil.isNotEmpty(request.getParameter("bpmStatus"))){
				query.eq("bpmStatus",request.getParameter("bpmStatus"));
			}
//			if (StringUtil.getStringLen(username)>20){
//				query.eq("createBy",username);
//			}else {
//				TSUser task = fxjUtil.getsysorgcode(username);
//				if (task != null) {
//					orgcode = task.getCurrentDepart().getSysOrgCode();
//				}
// 				query.like("sysOrgCode", orgcode + "%");
// 			}
			TSUser task = fxjUtil.getsysorgcode(username);

//			if(task!=null){
//				query.eq("diyaUser",task.getUserName());
//			}
			if(task!=null){
				query.eq("guohuUser",task.getUserName());
			}
			query.eq("maBy2","N");
		}catch (Exception e){
		}
		query.add();
		List<FxjLoanMaEntity> listsize = this.fxjLoanMaService.getListByCriteriaQuery(query,false);
		query.setCurPage(pageNumber<=0?1:pageNumber);
		query.setPageSize(pageSize<1?1:pageSize);
		query.add();
		List<FxjLoanMaEntity> listFxjLoanMas = this.fxjLoanMaService.getListByCriteriaQuery(query,true);
		return Result.success(listFxjLoanMas,(long)listsize.size());
	}

	@RequestMapping(value="/listgha/{username}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="融资物管理列表信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<FxjLoanMaEntity>> listgha(@PathVariable("username") String username, @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize > Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		CriteriaQuery query = new CriteriaQuery(FxjLoanMaEntity.class);

		TSUser tsUser = systemService.findUniqueByProperty(TSUser.class,"userName",username);
		String orgcode = "";
		try{
			if(StringUtil.isNotEmpty(request.getParameter("fiName"))){
				query.like("fiName","%"+request.getParameter("fiName")+"%");
			}

			if(StringUtil.isNotEmpty(request.getParameter("orderNo"))){
				query.like("orderNo","%"+request.getParameter("orderNo")+"%");
			}

			if(StringUtil.isNotEmpty(request.getParameter("bpmStatus"))){
				query.eq("bpmStatus",request.getParameter("bpmStatus"));
			}
//			if (StringUtil.getStringLen(username)>20){
//				query.eq("createBy",username);
//			}else {
//				TSUser task = fxjUtil.getsysorgcode(username);
//				if (task != null) {
//					orgcode = task.getCurrentDepart().getSysOrgCode();
//				}
// 				query.like("sysOrgCode", orgcode + "%");
// 			}
			TSUser task = fxjUtil.getsysorgcode(username);

//			if(task!=null){
//				query.eq("diyaUser",task.getUserName());
//			}
			if(task!=null){
				query.eq("guohuUser",task.getUserName());
			}
//			query.eq("maBy2","N");
		}catch (Exception e){
		}
		query.add();
		List<FxjLoanMaEntity> listsize = this.fxjLoanMaService.getListByCriteriaQuery(query,false);
		query.setCurPage(pageNumber<=0?1:pageNumber);
		query.setPageSize(pageSize<1?1:pageSize);
		query.add();
		List<FxjLoanMaEntity> listFxjLoanMas = this.fxjLoanMaService.getListByCriteriaQuery(query,true);
		return Result.success(listFxjLoanMas,(long)listsize.size());
	}


	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="根据ID获取融资物管理信息",notes="根据ID获取融资物管理信息",httpMethod="GET",produces="application/json")
	public ResponseMessage<?> get(@ApiParam(required=true,name="id",value="ID")@PathVariable("id") String id) {
		FxjLoanMaEntity task = null;
		try{
			task = fxjLoanMaService.get(FxjLoanMaEntity.class, Integer.parseInt(id));

		}catch (Exception e){

		}
		if (task == null) {
			try{
				task = fxjLoanMaService.findUniqueByProperty(FxjLoanMaEntity.class,"orderNo",id);

			}catch (Exception e){

			}
			if (task == null) {
				return Result.error("根据ID获取订单信息为空");
			}
		}
		return Result.success(task);
	}

	@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiOperation(value="创建融资物管理")
	public ResponseMessage<?> create(@ApiParam(name="融资物管理对象")@RequestBody FxjLoanMaEntity fxjLoanMa, UriComponentsBuilder uriBuilder) {
		//调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<FxjLoanMaEntity>> failures = validator.validate(fxjLoanMa);
		if (!failures.isEmpty()) {
			return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
		}

		//保存
		try{
			fxjLoanMaService.save(fxjLoanMa);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("融资物管理信息保存失败");
		}
		return Result.success(fxjLoanMa);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiOperation(value="更新融资物管理",notes="更新融资物管理")
	public ResponseMessage<?> update(@ApiParam(name="融资物管理对象")@RequestBody FxjLoanMaEntity fxjLoanMa) {
		//调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<FxjLoanMaEntity>> failures = validator.validate(fxjLoanMa);
		if (!failures.isEmpty()) {
			return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
		}

		//保存
		try{
            fxjLoanMa.setFiOperDate(now());
			FxjLoanMaEntity t = fxjLoanMaService.get(FxjLoanMaEntity.class, fxjLoanMa.getId());
			FxjOrderLogEntity fxjOrderLogEntity = new FxjOrderLogEntity();
			fxjOrderLogEntity.setOrderNo(t.getOrderNo());
			fxjOrderLogEntity.setLogUser(fxjLoanMa.getUpdateBy());
			fxjOrderLogEntity.setLogDate(now());
			fxjOrderLogEntity.setSysOrgCode(t.getSysOrgCode());
//			fxjOrderLogList.add(fxjOrderLogEntity);
			fxjOrderLogEntity.setBpmStatus(fxjLoanMa.getBpmStatus());
			try{
				fxjOrderLogEntity.setLogContent("抵押品操作");//审批意见
			}catch (Exception e){

			}
			systemService.save(fxjOrderLogEntity);
			MyBeanUtils.copyBeanNotNull2Bean(fxjLoanMa, t);
			fxjLoanMaService.saveOrUpdate(t);


			fxjUtil.changeBpmstatus(fxjLoanMa.getOrderNo(),fxjLoanMa.getBpmStatus());
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("更新融资物管理信息失败");
		}

		//按Restful约定，返回204状态码, 无内容. 也可以返回200状态码.
		return Result.success("更新融资物管理信息成功");
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.NO_CONTENT)
	@ApiOperation(value="删除融资物管理")
	public ResponseMessage<?> delete(@ApiParam(name="id",value="ID",required=true)@PathVariable("id") String id) {
		logger.info("delete[{}]" , id);
		// 验证
		if (StringUtils.isEmpty(id)) {
			return Result.error("ID不能为空");
		}
		try {
			fxjLoanMaService.deleteEntityById(FxjLoanMaEntity.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("融资物管理删除失败");
		}

		return Result.success();
	}
}
